﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace IceDog.TaskFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            Run1();
            Run2();
        }
        private static void Run1()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int path = 1;

            List<int> numbers = new List<int>(Enumerable.Range(0, 10000));

            if (path == 1)
            {
                Parallel.ForEach(numbers, x =>
                {
                    Console.WriteLine(x);
                    Thread.Sleep(1);

                });
            }
            else
            {
                ThreadFactory workers = new ThreadFactory(10, numbers.Count, (min, max, text) =>
                {

                    for (int i = min; i <= max; i++)
                    {
                        Console.WriteLine(numbers[i]);
                        Thread.Sleep(1);
                    }
                });

                workers.StartWorking();
            }

            watch.Stop();
            Console.WriteLine(watch.Elapsed.TotalSeconds.ToString());

            Console.ReadLine();
        }

        private static void Run2()
        {
            var path = 1;
            var cieling = 10000000;
            List<int> numbers = new List<int>();

            if (path == 1)
            {
                Parallel.For(0, cieling, x =>
                {
                    lock (numbers)
                    {
                        numbers.Add(x);
                    }

                });
            }

            else
            {
                ThreadFactory workers = new ThreadFactory(10, cieling, (min, max, text) =>
                {

                    for (int i = min; i <= max; i++)
                    {
                        lock (numbers)
                        {
                            numbers.Add(i);
                        }

                    }
                });

                workers.StartWorking();
            }
        }
    }
}
